<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <%@include file="/common/common.jsp" %>
    <link rel="stylesheet" href="${basePath}/css/layerPage.css${VERSION}"/>
    <link rel="stylesheet" href="${basePath}/assets/css/foundation-datepicker.css"/>
	<script type="text/javascript" src="${basePath}/assets/js/foundation-datepicker.js"></script>
</head>
<body>
<form id="form" name="form" class="form-horizontal" method="post" action="${basePath}/task/addTask">
    <div class="panel-body">
        <div class="form-group">
            <label class="col-sm-3 control-label">任务名称：</label>
            <div class="col-sm-9">
                <input type="text" class="col-sm-5" id="title" name="title"/>
                
                <!--  input type="hidden" class="col-sm-5" id="aStartPl" name="aStartPl"/>
                <input type="hidden" class="col-sm-5" id="aEndPl" name="aEndPl"/>
                <input type="hidden" class="col-sm-5" id="aDesc" name="aDesc"/>-->
                
                <c:if test="${taskPid!='0' and taskPid!=null}">
    				<input type="hidden" id="taskPid" name="taskPid" value="${taskPid}"/>
    				<input type="hidden" id="taskPname" name="taskPname" value="${taskPname}"/>
    			</c:if>
    			<c:if test="${taskPuid!='0' and taskPuid!=null}">
    				<input type="hidden" id="taskPuid" name="taskPuid" value="${taskPuid}"/>
    			</c:if>
            </div>
        </div>
        
        
        <div class="form-group">
            <label class="col-sm-3 control-label">任务种类：</label>
            <div class="col-sm-9" style="width:200px">
                <select id="taskCla" name="taskCla" class="selectpicker show-tick form-control">
                	
		          	<option value="66" selected >简单任务</option>
		          	<option value="22">工作日志</option>
		         	<option value="33">蓝墙</option>
        		</select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">任务类型：</label>
            <div class="col-sm-9" style="width:200px">
                <select id="taskType" name="taskType" class="selectpicker show-tick form-control">
                	
		          	<option value="1" selected >紧急重要</option>
		          	<option value="2">紧急不重要</option>
		          	<option value="3">不紧急重要</option>
		          	<option value="4" >不紧急不重要</option>
		          	<option value="5">其它</option>
        		</select>
            </div>
        </div>
        
        <div class="form-group">
            <label class="col-sm-3 control-label">计划开始时间：</label>
            <div class="col-sm-9">
                <input type="text" class="date-icon" id="startTimePl" name="startTimePl" readonly/>
                
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label"></label>
            <div class="col-sm-9">
                <button type="button" id="addDays" class="btn btn-danger">+</button>
                <button type="button" id="delDays" class="btn btn-danger">-</button>
                <button type="button" id="tomorrows" class="btn btn-danger">明天</button>
                <button type="button" id="acquireds" class="btn btn-danger">后天</button>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">计划结束时间：</label>
            <div class="col-sm-9">
                <input type="text" class="date-icon" id="endTimePl" name="endTimePl" readonly/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label"></label>
            <div class="col-sm-9">
                <button type="button" id="addDaye" class="btn btn-danger">+</button>
                <button type="button" id="delDaye" class="btn btn-danger">-</button>
                <button type="button" id="tomorrowe" class="btn btn-danger">明天</button>
                <button type="button" id="acquirede" class="btn btn-danger">后天</button>
            </div>
        </div>
        
        <c:choose>  
  
  			
	        
		   <c:when test="${taskPid>0}">
				<div class="form-group">
		            <label class="col-sm-3 control-label">选择人员：</label>
		            <div class="col-sm-9">
		            	<button type="button" class="btn btn-primary btn-lg" id="taskUserShow">
						  <span class="glyphicon glyphicon-user"></span> 打开人员
						</button>
		                <button type="button" class="btn btn-primary btn-lg" id="taskUserClose">
						  <span class="glyphicon glyphicon-user"></span> 关闭人员
						</button>
		            </div>
		        </div>
				<div class="panel panel-default" id="panel-body-1">
			        
			        <div class="form-group" style="padding-top:10px">
			            <label class="col-sm-3 control-label">分配人员：</label>
			            <div class="col-sm-9">
			                <input class="col-sm-5" type="text" name="userName" id="userName" readonly="true"/>
			                <input type="hidden" name="alloterId" id="alloterId"/>
			                <input type="hidden" name="alloterIds" id="alloterIds"/>
			                <button type="button" id="chooseUserBtn" class="btn btn-danger">选择人员</button>
			            </div>
			        </div>
			        <div class="form-group">
			            <label class="col-sm-3 control-label">人员计划开始时间：</label>
			            <div class="col-sm-9">
			                <input type="text" class="date-icon" id="aStartPl" name="aStartPl" readonly/>
			            </div>
			        </div>
			        
			        <div class="form-group">
			            <label class="col-sm-3 control-label"></label>
			            <div class="col-sm-9">
			                <button type="button" id="addDaysAs" class="btn btn-danger">+</button>
			                <button type="button" id="delDaysAs" class="btn btn-danger">-</button>
			                <button type="button" id="tomorrowsAs" class="btn btn-danger">明天</button>
			                <button type="button" id="acquiredsAs" class="btn btn-danger">后天</button>
			            </div>
			        </div>
			
			        <div class="form-group">
			            <label class="col-sm-3 control-label">人员计划结束时间：</label>
			            <div class="col-sm-9">
			                <input type="text" class="date-icon" id="aEndPl" name="aEndPl" readonly/>
			            </div>
			        </div>
			        
			         <div class="form-group">
			            <label class="col-sm-3 control-label"></label>
			            <div class="col-sm-9">
			                <button type="button" id="addDaysAe" class="btn btn-danger">+</button>
			                <button type="button" id="delDaysAe" class="btn btn-danger">-</button>
			                <button type="button" id="tomorrowsAe" class="btn btn-danger">明天</button>
			                <button type="button" id="acquiredsAe" class="btn btn-danger">后天</button>
			            </div>
			        </div>
			        
			        <div class="form-group">
			            <label class="col-sm-3 control-label">描述：</label>
			            <div class="col-sm-9">
			                <textarea id="aDesc" name="aDesc" rows="5" cols="30"></textarea>
			            </div>
			        </div>
			       
		    	</div>
				
				
		   </c:when>  
		     
		   <c:otherwise>  
				<div class="form-group" id="taskUser">
		            <label class="col-sm-3 control-label">分配任务：</label>
		            <div class="col-sm-9" style="width:120px">
		                <input type="hidden" name="alloterId" id="alloterId"/>
		                <input type="hidden" name="alloterIds" id="alloterIds"/>
		    			<input class="col-sm-5" type="text" name="userName" id="userName" data-toggle="dropdown" readonly value= "请选择人员" style="width:400px"/>
					    <ul class="dropdown-menu" role="menu" aria-labelledby="userName">
					    	<c:forEach var="item" items="${userTaskVos}" varStatus="status">
						    	<li role="presentation" >
						            <a id="menus" role="menuitem" tabindex="${item.userId}" href="#"  onclick="javascript:subgo('${item.userName}','${item.userId}')">${item.userName}</a>
						        </li>
							</c:forEach> 
					    </ul>
		            </div>
		            <div class="col-sm-9">
		            <button type="button" id="chooseUserBtn" class="btn btn-danger">指定人员</button>
		            </div>
		        </div>
		   </c:otherwise>  
		</c:choose>  
        
        
        
        <div class="form-group">
            <label class="col-sm-3 control-label">描述：</label>
            <div class="col-sm-9">
                <textarea id="description" name="description" rows="5" cols="30" value=''></textarea>
            </div>
        </div>
        <button id="submit" type="submit" class="btn btn-primary btn-submit">提交</button>
    </div>
</form>
<script type="text/javascript">
	var first = 0;
	var taskUserIsO = 0;
	var startTimePl = $("#startTimePl").val();
	var endTimePl = $("#endTimePl").val();
	var userIds = [];
	
	$("#panel-body-1").hide();
	
	
	var subgo = function(userName,alloterId){
		//alert(userName);
		$('#userName').val(userName);
		$('#alloterIds').val(alloterId);
	}
	
	
    jQuery(function ($) {
    	//alert($("#form").attr("action","${basePath}/task/addTask?userIds=111111111"));
    	//日期处理开始
    	var mydate = new Date();
    	var str = "" + mydate.getFullYear() + "-";
    	str += ((mydate.getMonth()+1)<10?'0'+(mydate.getMonth()+1):(mydate.getMonth()+1)) + "-";
    	str += (mydate.getDate())<10?'0'+(mydate.getDate()):(mydate.getDate());
    	$("#startTimePl").val(str);
    	$("#endTimePl").val(str);
    	
    	$("#taskUserShow").click(function() {
    		taskUserIsO = 1;
    		$("#aStartPl").val(str);
        	//alert($("#aStartPl").val());
        	$("#aEndPl").val(str);
    		$("#panel-body-1").show();
		});
    	
    	$("#taskUserClose").click(function() {
    		taskUserIsO = 0;
    		$("#aStartPl").val();
            $("#aEndPl").val();
            $("#userName").val();
            $("#aDesc").val();
    		$("#panel-body-1").hide();
		});
    	
    	
    	
    	
    	
    	
    	

			// 最近一天单击事件
		$("#addDays").click(function() {
					$("#startTimePl").val(change_date(1,1));
				});

		$("#delDays").click(function() {
					$("#startTimePl").val(change_date(-1,1));
				});
			
			
		$("#addDaye").click(function() {
			$("#endTimePl").val(change_date(1,0));
		});

		$("#delDaye").click(function() {
			$("#endTimePl").val(change_date(-1,0));
		});	
		
		//人员展开时间时间开始
		$("#addDaysAs").click(function() {
			$("#aStartPl").val(change_datea(1,1));
		});

		$("#delDaysAs").click(function() {
			$("#aStartPl").val(change_datea(-1,1));
		});
			
			
		$("#addDaysAe").click(function() {
			$("#aEndPl").val(change_datea(1,0));
		});
		
		$("#delDaysAe").click(function() {
			$("#aEndPl").val(change_datea(-1,0));
		});	
		
		
		
		$("#tomorrowsAe").click(function() {
			$("#aEndPl").val(change_datea(1,2));
		});	
		
		$("#acquiredsAe").click(function() {
			$("#aEndPl").val(change_datea(2,2));
		});	
		
		$("#tomorrowsAs").click(function() {
			$("#aStartPl").val(change_datea(1,2));
		});	
		
		$("#acquiredsAs").click(function() {
			$("#aStartPl").val(change_datea(2,2));
		});	
		
		var change_datea = function(days,typ) {
			// 参数表示在当前日期下要增加的天数
			var now;
			if(typ==1){
				now = new Date($("#aStartPl").val());
			}else if(typ==0){
				now = new Date($("#aEndPl").val());
			}else{
				now = new Date();
			}

			// + 1 代表日期加，- 1代表日期减
			now.setDate((now.getDate() + days));
			
			var year = now.getFullYear();
			var month = now.getMonth() + 1;
			var day = now.getDate();
			if (month < 10) {
				month = '0' + month;
			}
			if (day < 10) {
				day = '0' + day;
			}

			return year + '-' + month + '-' + day;
		};
		
		
		//人员展开时间时间结束
		
		
		
		
		$("#tomorrowe").click(function() {
			$("#endTimePl").val(change_date(1,2));
		});	
		
		$("#acquirede").click(function() {
			$("#endTimePl").val(change_date(2,2));
		});	
		
		$("#tomorrows").click(function() {
			$("#startTimePl").val(change_date(1,2));
		});	
		
		$("#acquireds").click(function() {
			$("#startTimePl").val(change_date(2,2));
		});	
		
		
		
		var change_date = function(days,typ) {
			// 参数表示在当前日期下要增加的天数
			var now;
			if(typ==1){
				now = new Date($("#startTimePl").val());
			}else if(typ==0){
				now = new Date($("#endTimePl").val());
			}else{
				now = new Date();
			}

			// + 1 代表日期加，- 1代表日期减
			now.setDate((now.getDate() + days));
			
			var year = now.getFullYear();
			var month = now.getMonth() + 1;
			var day = now.getDate();
			if (month < 10) {
				month = '0' + month;
			}
			if (day < 10) {
				day = '0' + day;
			}

			return year + '-' + month + '-' + day;
		};

		
		
		
		
		//日期处理结束
    	
    	
    	$("#chooseUserBtn").click("click", function () {
		
            //alert(taskTitle);
            
            if($("#startTimePl").val()==''){
            	layer.alert('请先填写任务计划开始时间！');
            	return;
            }
            
            if($("#endTimePl").val()==''){
            	layer.alert('请先填写任务计划结束时间！');
            	return;
            }
            
            var startTimePl1 = new Date(Date.parse($("#startTimePl").val().replace("-", "/")));
            var endTimePl2 = new Date(Date.parse($("#endTimePl").val().replace("-", "/")));
        	if(startTimePl1>endTimePl2){
        		layer.alert('任务计划开始时间应小于等于任务的计划开始时间！');
        		return;
        	}
        	openOrg();
            //first = 1;
            //pageii = layer.open({
                //title: "任务分配",
                //type: 2,
                //area: ["800px", "500px"],
                //content: '${basePath}/task/taskAllot'
            //});
        });

        //选择部门
        function openOrg() {       	
            var pageii = layer.open({
                title: false,
                type: 2,
                area: ["800px", "600px"],
                content: ['${basePath}/task/anon/toOrgTree']//,
                //btn: ["确认", "关闭"],
                //yes: function (index, layero) {
                	//var userName = $(layero).find("iframe")[0].contentWindow.userName;
                    //var alloterId = $(layero).find("iframe")[0].contentWindow.alloterId;
                    //layer.close(index);
                    //$("#userName").val(userName);
                    //$("#alloterId").val(alloterId);
                //}, cancel: function () {
                    //return true;
                //}
            });
        }


		//任务计划开始时间须小于等于任务计划结束时间
    	jQuery.validator.methods.compareDate = function(value, element, param) {
            var startDate = jQuery(param).val();
          //  var endDate = element.attr(value);
//            var date1 = new Date(startDate.replace("-", "/"));
//            var date2 = Date.parse(value.replace("-", "/"));
            alert("startDate"+startDate);
            alert("endDate"+endDate);
            alert("value"+ value);

           alert("result:"+  date1.getTime() <= date2.getTime());
            return startDate<value;
        };
        
      //人员任务计划开始时间须大于等于总任务计划开始时间
    	jQuery.validator.methods.compareDateTaskUser = function(value, element, param) {
            var startDate = jQuery(param).val();
//            //alert(param+"--"+value);
//            var dates = new Date(Date.parse(startDate.replace("-", "/")));
//            var datee = new Date(Date.parse(value.replace("-", "/")));
            return startDate >= value;
        };



        $('.date-icon').fdatepicker({
            format: 'yyyy-mm-dd'
        });

        $("#startTimePl").change(function(){
            var startDate = $("#startTimePl").val();
            var endDate = $("#endTimePl").val();
            if(startDate >endDate) {
                if($("#endId")!= null)  $("#endId").remove();
                if($("#startId")!= null)
                $("#startTimePl").after("<label id='startId'><font color='red'>计划开始时间不能大于计划结束时间</font></label>");
            }else{
                $("#startId").remove();
            }
        });
        $("#endTimePl").change(function(){
            var startDate = $("#startTimePl").val();
            var endDate = $("#endTimePl").val();
            if(startDate >endDate) {
                if($("#startId")!= null)  $("#startId").remove();
                if ($("#endId") != null)
                    $("#endTimePl").after("<label id='endId'><font color='red'>计划结束时间不能小于计划开始时间</font></label>");
             }else{
            $("#endId").remove();
        }
        });

        $("#aStartPl").change(function(){
            var startDate = $("#startTimePl").val();
            var endDate = $("#aStartPl").val();
            if(startDate >endDate) {
                if($("#startId")!= null)  $("#startId").remove();
                if ($("#startPId") != null)
                    $("#aStartPl").after("<label id='startPId'><font color='red'>人员计划开始日期必须大于等于计划开始日期</font></label>");
            }else{
                $("#startPId").remove();
            }
        });

    	
    	
        var $form = $("#form");
        var $title = $("#title");
        var $startTimePl = $("#startTimePl");
        var $endTimePl = $("#endTimePl");
        var $aStartPl = $("#aStartPl");
        var $aEndPl = $("#aEndPl");
        var $userName = $("#userName");
        var $aDesc = $("#aDesc");
        
        
        var $description = $("#description");
        var $submit = $("#submit");
		
        
        if(taskUserIsO==0){
	        $form.validate({
	            errorClass: 'error-msg',
	            rules: {
	            	title:{
	                    required:true,
	                    remote: {
	                        type: "post",
	                        url: "${basePath}/task/anon/checkTitle",
	                        data: {
	                        	title: function() {
	                                return $("#title").val();
	                            }
	                        },
	                        dataType: "json",
	                        dataFilter: function(data, type) {
	                            var json = eval('('+data+')');
	                            if(json.status == '0'){
	                                return true;
	                            } else {
	                                return false;
	                            }
	                        }
	                    }
	                },
	                startTimePl: {
	                    required: true
	                },
	                endTimePl: {
	                    required: true
//	                    compareDate: "#startTimePl"
	                },
	                
	                aStartPl:{
	                    required: true
//	                    compareDate: "#startTimePl"
	                },
	                aEndPl:{
	                    required: true,
//	                    compareDateTaskUser:"#endTimePl",
//	                    compareDate: "#endTimePl"
	                },
	                userName: {
	                    required: true
	                },
	                description:{
	                	rangelength:[0,500]
	                } 
	            },
	            messages: {
	            	title: {
	                    required: "标题必填*",
	                    remote: jQuery.format("标题已存在*")
	                },
	                userName: {
	                    required: "人员必填*"
	                },
	                startTimePl: "计划开始时间必填*",
	                endTimePl:{
	                	required:"计划结束时间必填*"
//	                	compareDate:"计划结束日期必须大于等于计划开始日期!"
	                },
	                aStartPl: {
	                	required:"人员计划开始时间必填*"
//	                	compareDate:"人员计划开始日期必须大于等于计划开始日期!",
	                },
	                aEndPl:{
	                	required:"人员计划结束时间必填*"
//	                	compareDate:"人员计划结束日期必须大于等于人员计划开始日期!",
//	                	compareDateTaskUser:"人员计划结束日期必须小于等于总任务计划结束日期!"
	                },
	                description:"任务说明长度不能超过100！*",
	                aDesc:"分配人员说明长度不能超过100！*"
	            },
	            submitHandler: function (form) {
	            	
	                $.ajax({
	                    url: $form.attr("action"),
	                    type: $form.attr("method"),
	                    data: $form.serialize(),
                        async:false,
	                    dataType: "json",
	                    cache: false,
	                    beforeSend: function () {
	                        $submit.prop("disabled", true);
	                    },
	                    success: function (data) {
	                    	//alert("123:"+data);
	                        if (data.status == '0') {
	                            $submit.prop("disabled", false);
	                            layer.confirm('任务添加成功！是否关闭窗口？', {icon: 1}, function (index) {
	                                parent.$("#grid-table").jqGrid().trigger("reloadGrid", [{page: 1}]);
	                                parent.layer.close(parent.pageii);
	                                return false
	                            });
	                        } else {
	                            $submit.prop("disabled", false);
	                            layer.alert(data, {
	                                icon: 0,
	                                skin: 'layui-layer-lan',
	                                closeBtn: 0
	                            });
	                        }
	                    },
	                    error: function (xhr, textStatus, errorThrown) {
	                        $submit.prop("disabled", false);
	                        layer.alert("系统未知错误", {
	                            icon: 0,
	                            skin: 'layui-layer-lan',
	                            closeBtn: 0
	                        });
	                    }
	                });
	            }
	        });
		}else{
			$form.validate({
	            errorClass: 'error-msg',
	            rules: {
	            	title:{
	                    required:true,
	                    remote: {
	                        type: "post",
	                        url: "${basePath}/task/anon/checkTitle",
	                        data: {
	                        	title: function() {
	                                return $("#title").val();
	                            }
	                        },
	                        dataType: "json",
	                        dataFilter: function(data, type) {
	                            var json = eval('('+data+')');
	                            if(json.status == '0'){
	                                return true;
	                            } else {
	                                return false;
	                            }
	                        }
	                    }
	                },
	                startTimePl: {
	                    required: true
	                },
	                endTimePl: {
	                    required: true,
//	                    compareDate: "#startTimePl"
	                },
	                description:{
	                	rangelength:[0,100]
	                } 
	            },
	            messages: {
	            	title: {
	                    required: "标题必填*",
	                    remote: jQuery.format("标题已存在*")
	                },
	                startTimePl: "计划开始时间必填*",
	                endTimePl:{
	                	required:"计划结束时间必填*"
	          //      	compareDate:"计划结束日期必须大于等于计划开始日期!"
	                },
	                description:"任务说明长度不能超过100！*"
	            },
	            submitHandler: function (form) {
	            	
	                $.ajax({
	                    url: $form.attr("action"),
	                    type: $form.attr("method"),
	                    data: $form.serialize(),
                        async:false,
	                    dataType: "json",
	                    cache: false,
	                    beforeSend: function () {
	                        $submit.prop("disabled", true);
	                    },
	                    success: function (data) {
                          //  alert("234:"+data);
	                        if (data.status == '0') {
	                            $submit.prop("disabled", false);
	                            layer.confirm('任务添加成功！是否关闭窗口？', {icon: 1}, function (index) {
	                                parent.$("#grid-table").jqGrid().trigger("reloadGrid", [{page: 1}]);
	                                parent.layer.close(parent.pageii);
	                                return false
	                            });
	                        } else {
	                            $submit.prop("disabled", false);
	                            layer.alert(data, {
	                                icon: 0,
	                                skin: 'layui-layer-lan',
	                                closeBtn: 0
	                            });
	                        }
	                    },
	                    error: function (xhr, textStatus, errorThrown) {
	                        $submit.prop("disabled", false);
	                        layer.alert("系统未知错误", {
	                            icon: 0,
	                            skin: 'layui-layer-lan',
	                            closeBtn: 0
	                        });
	                    }
	                });
	            }
	        });
        	
        }

    });
</script>
</body>
</html>
